﻿// ************************************************* //
// FilesOperator.cs
// Author: Chris Wu
// Email: cwu@dataio.com
// ------------------------------------ //
// version 1.0 - Initial Create
// date: March 25th, 2010
// ------------------------------------ //
//
// ************************************************* //

using System;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace Common
{
    /// <summary>
    /// Description of FilesOperator.
    /// </summary>
    public class FilesOperator
	{
        private int m_AdapterId;            // adapter id
        private string m_AdapterName;       // adapter name
        private int m_TotalPins;            // total pins
        private string m_PackageName;       // package name
        private int m_RowNum;               // row num
        private int m_ColumnNum;            // column num
        private int m_ListSize;             // list size
        private string[] m_PositionList;    // position list
        private string[] m_LabelList;       // label list

        // Construtor
        public FilesOperator()
        { }

        // get and set m_AdapterId
        public int AdapterId
        {
            get { return m_AdapterId; }
            set { m_AdapterId = value; }
        }

        // get and set m_AdapterName
        public string AdapterName
        {
            get { return m_AdapterName; }
            set { m_AdapterName = value; }
        }

        // get and set m_TotalPins
        public int TotalPins
        {
            get { return m_TotalPins; }
            set { m_TotalPins = value; }
        }

        // get and set m_PackageName
        public string PackageName
        {
            get { return m_PackageName; }
            set { m_PackageName = value; }
        }

        // get and set m_RowNum
        public int RowNum
        {
            get { return m_RowNum; }
            set { m_RowNum = value; }
        }

        // get and set m_ColumnNum
        public int ColumnNum
        {
            get { return m_ColumnNum; }
            set { m_ColumnNum = value; }
        }

        // get and set m_ListSize
        public int ListSize
        {
            get { return m_ListSize; }
            set { m_ListSize = value; }
        }

        // get and set m_PositionList
        public string[] PositionList
        {
            get { return m_PositionList; }
            set { m_PositionList = value; }
        }

        // get and set m_LabelList
        public string[] LabelList
        {
            get { return m_LabelList; }
            set { m_LabelList = value; }
        }

        // output the parameter files
        public void OutputParamFiles()
        {
            // using stream to export to txt
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "TXT files (*.txt) | *.txt";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Output Pin Map as TXT files";
            saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter srw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            int i;
            // int j;
            char[] mychars;

            srw.WriteLine();
            srw.WriteLine("Detailed Information for Adapter Name " + m_AdapterName);
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine("*******************************");
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine("Total Pin Numbers: " + m_TotalPins);
            srw.WriteLine();
            srw.WriteLine("Package Information: " + m_PackageName);
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine("*******************************");

            // prepare for HDbS_Dat.cpp
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();
            for (i = 0; i < m_ListSize; i++)
            {
                if ("" != m_LabelList[i])
                {
                    srw.WriteLine("\tHDT::SKT_" + m_LabelList[i] + ",\t\t// pin " + m_PositionList[i]);
                }
            }

            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine("*******************************");
            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();

            // prepare for Params.cpp
            for (i = 0; i < m_ListSize; i++)
            {
                if ("" != m_LabelList[i])
                {

                    mychars = m_LabelList[i].ToCharArray();
                    if ('N' == mychars[0])
                    {
                        srw.WriteLine("\tNOCONN,\t\tNO_MODE,\t// pin " + m_PositionList[i]);
                    }
                    else if ('G' == mychars[0] && 'N' == mychars[1])
                    {
                        srw.WriteLine("\tGND,\t\tGND_RES,\t// pin " + m_PositionList[i]);
                    }
                    else if ('G' == mychars[0])
                    {
                        srw.WriteLine("\tGP" + mychars[1].ToString() + ",\t\tMANUAL,\t\t// pin " + m_PositionList[i]);
                    }
                    else if ('V' == mychars[0])
                    {
                        srw.WriteLine("\tVCC,\t\tVCC_RES,\t// pin " + m_PositionList[i]);
                    }
                    else
                    {
                        srw.WriteLine("\t" + m_LabelList[i] + ",\t\tBUS,\t\t// pin " + m_PositionList[i]);

                    }
                }
            }

            srw.WriteLine();
            srw.WriteLine();
            srw.WriteLine();

            srw.WriteLine("You might need some little modifications for some special pins.");
            srw.WriteLine();
            srw.WriteLine("Good Luck!");

            srw.Close();
            myStream.Close();
            MessageBox.Show("Please check " + saveFileDialog.FileName + "!", "Informer");
            return;
        }

        // save as *.pin files
        public void SaveAsPinFiles()
        {
            // MessageBox.Show("Save Pin Definition default file");
            // save the pin definition information into a default file *.pin
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "PIN files (*.pin) | *.pin";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Save Pin Map as PIN files";
            saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter srw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));

            srw.WriteLine("Adapter ID:");
            srw.WriteLine(m_AdapterId);
            srw.WriteLine("Adapter Name:");
            srw.WriteLine(m_AdapterName);
            srw.WriteLine("Total Pins:");
            srw.WriteLine(m_TotalPins);
            srw.WriteLine("Package Name:");
            srw.WriteLine(m_PackageName);
            srw.WriteLine("Row:");
            srw.WriteLine(m_RowNum);
            srw.WriteLine("Column:");
            srw.WriteLine(m_ColumnNum);
            srw.WriteLine("List Size:");
            srw.WriteLine(m_ListSize);

            srw.WriteLine("Data:");
            int i;
            for (i = 0; i < m_ListSize; i++)
            {
                if ("" != m_LabelList[i])
                {
                    srw.WriteLine(i);
                    srw.WriteLine(m_PositionList[i]);
                    srw.WriteLine(m_LabelList[i]);
                }
            }
            
            srw.Close();
            myStream.Close();
            MessageBox.Show("We saved to " + saveFileDialog.FileName + "!", "Informer");
        }

        // load *.pin files
        public void LoadPinFiles()
        {
            // open the pin definition information into a default file *.pin
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PIN files (*.pin) | *.pin";
            openFileDialog.FilterIndex = 0;
            openFileDialog.RestoreDirectory = true;
            // openFileDialog.CreatePrompt = true;
            openFileDialog.Title = "Open Pin Map as PIN files";
            openFileDialog.ShowDialog();

            if (openFileDialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            Stream myStream;
            myStream = openFileDialog.OpenFile();
            StreamReader srw = new StreamReader(myStream, System.Text.Encoding.GetEncoding(-0));
            string lstr;

            lstr = srw.ReadLine();
            if ("Adapter ID:" == lstr)
            {
                lstr = srw.ReadLine();
                m_AdapterId = int.Parse(lstr);
            }
            lstr = srw.ReadLine();
            if ("Adapter Name:" == lstr)
            {
                lstr = srw.ReadLine();
                m_AdapterName = lstr;
            }
            lstr = srw.ReadLine();
            if ("Total Pins:" == lstr)
            {
                lstr = srw.ReadLine();
                m_TotalPins = int.Parse(lstr);
            }
            lstr = srw.ReadLine();
            if ("Package Name:" == lstr)
            {
                lstr = srw.ReadLine();
                m_PackageName = lstr;
            }
            lstr = srw.ReadLine();
            if ("Row:" == lstr)
            {
                lstr = srw.ReadLine();
                m_RowNum = int.Parse(lstr);
            }
            lstr = srw.ReadLine();
            if ("Column:" == lstr)
            {
                lstr = srw.ReadLine();
                m_ColumnNum = int.Parse(lstr);
            }
            lstr = srw.ReadLine();
            if ("List Size:" == lstr)
            {
                lstr = srw.ReadLine();
                m_ListSize = int.Parse(lstr);
            }
            m_PositionList = new string[m_ListSize];
            m_LabelList = new string[m_ListSize];
            lstr = srw.ReadLine();
            if ("Data:" == lstr)
            {
                lstr = srw.ReadLine();
                for (int i = 0; i < m_ListSize; i++)
                {
                    if (null != lstr && i == int.Parse(lstr))
                    {
                        lstr = srw.ReadLine();
                        m_PositionList[i] = lstr;
                        lstr = srw.ReadLine();
                        m_LabelList[i] = lstr;
                        lstr = srw.ReadLine();
                    }
                    else
                    {
                        m_PositionList[i] = "";
                        m_LabelList[i] = "";
                    }
                }
            }
            
            srw.Close();
            myStream.Close();
            
            return;
        }

        // output pads template file
        public void OutputPadTemplate()
        {
            // save the pinout list as Excel files
            int i;
            int orders = 0;

            // using stream to export to excel
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel files (*.xls) | *.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Save as Excel files";
            saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string contents, ncstr;
            contents = "";
            ncstr = "Unused Pin";

            try
            {
                // move data into excel
                contents = "Pin Group\tNumber\tName\tType\tSwap\tSeq.";
                sw.WriteLine(contents);
                for (i = 0; i < m_ListSize; i++)
                {
                    contents = "";
                    if ("" != m_LabelList[i] && "NC" != m_LabelList[i])
                    {
                        orders++;
                        contents += "Gate-A\t";
                        contents += m_PositionList[i] + "\t";
                        contents += m_LabelList[i] + "\t";
                        contents += "Undefined\t0\t";
                        contents += orders.ToString();
                        sw.WriteLine(contents);
                    }
                    else
                    {
                        contents += ncstr + "\t";
                        contents += m_PositionList[i];
                        sw.WriteLine(contents);
                    }
                }
                sw.Close();
                myStream.Close();
                // MessageBox.Show("Please check " + saveFileDialog.FileName + "!", "Informer");
            }
            catch //(Exception e)
            {
                //MessageBox.Show(e.ToString());
            }
            finally
            {
                sw.Close();
                myStream.Close();
                //MessageBox.Show("Please check " + saveFileDialog.FileName + "!", "Informer");
            }
        }

        // save as excel file
        public void SaveAsExcelFiles()
        {
            // using stream to export to excel
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Excel files (*.xls) | *.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Save as Excel files";
            saveFileDialog.ShowDialog();

            if (saveFileDialog.FileName.IndexOf(":") < 0)
            {   // if click cancel
                return;
            }

            Stream myStream;
            myStream = saveFileDialog.OpenFile();
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));

            // sw.WriteLine("Welcome to C# world, Chris.");
            sw.WriteLine();
            sw.WriteLine("Detailed Information for Adapter Name " + m_AdapterName);
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("--------------------------------------------------------");
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("Total Pin Numbers: " + m_TotalPins);
            sw.WriteLine();
            sw.WriteLine("Package Information: " + m_PackageName);
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("--------------------------------------------------------");
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine();
            
            //string contents = "";

            //try
            //{
            //    // move data into excel
            //    for (i = 0; i < m_ListSize; i++)
            //    {
            //        contents = "\t\t";
            //        for (j = 0; j < column; j++)
            //        {
            //            if (j > 0)
            //            {
            //                contents += "\t\t";
            //            }

            //            if (dataGridView2[j, i].Value != null)
            //            {
            //                contents += dataGridView2[j, i].Value.ToString();
            //            }
            //            else
            //            {
            //                contents += "";
            //            }
            //        }

            //        // write data
            //        sw.WriteLine(contents);
            //        sw.WriteLine();
            //    }
            //    sw.Close();
            //    myStream.Close();
            //    // MessageBox.Show("Please check " + saveFileDialog.FileName + "!", "Informer");
            //}
            //catch //(Exception e)
            //{
            //    MessageBox.Show(e.ToString());
            //}
            //finally
            //{
            //    sw.Close();
            //    myStream.Close();
            //    MessageBox.Show("Please check " + saveFileDialog.FileName + "!", "Informer");
            //}
        }
	}
}
